function demo( exnum )
% demo - example use of the code in this directory
%

%-----------------------------------------------------------
% Gather the patches from the images...
%-----------------------------------------------------------
global X1 X2; 

switch exnum,
case 1,
    %-----------------------------------------------------------
    % LARGE - Topographic ICA (model for complex cells and topography)
    %-----------------------------------------------------------
    
    p.seed = 1;
    p.write = 1;
    p.update = 10;
    p.maxIter = 100;
    p.xdim = 16;
    p.ydim = 16;
    p.maptype = 'torus'; % identical / torus
    p.neighborhood = 'ones3by3';
    p.stepsize = 0.1;
    p.epsi = 0.005;
    p.mode = 'GPU';
    p.gpuID = 0;
    p.gpuIter = 10;
    p.orthoMode = 'decorrelate'; % decorrelate/orthogonalize
    p.maxDecoIter = 1000;
    p.decoEpsi = 1e-3;
    p.localOrthoGamma = 1;
    p.globalOrthoGamma = 1;
    p.orthoEpsi = 1;
    
    if strcmp(p.mode,'gpu') | strcmp(p.mode,'GPU')
        fprintf('GPU initialize\n');
        matCudaXTICA('initial', int32(p.gpuID));
    end
    
    [X1, whiteningMatrix1, dewhiteningMatrix1] = data( 10000, 16, 256, '../data1/', p.mode );
    [X2, whiteningMatrix2, dewhiteningMatrix2] = data( 10000, 16, 256, '../data2/', p.mode );
        
    estimate( whiteningMatrix1, whiteningMatrix2, dewhiteningMatrix1, dewhiteningMatrix2, '../results/xica.mat', p );

case 2,

    %-----------------------------------------------------------
    % LARGE - Displaying the estimated bases
    %-----------------------------------------------------------

    load ../results/xica.mat;
    visual( A1, 2, 8 );
    visual( W1', 2, 8 );
    figure; plot(objiter1, obj1);
    set(gca,'FontSize',24);
    visual( A2, 2, 8 ); 
    visual( W2', 2, 8 );
    figure; plot(objiter2, obj2);
    set(gca,'FontSize',24);

case 3,

    load ../results/xica.mat; 

    [xi,yi] = meshgrid(-8:7,-8:7); 
    
    W1_theta_list = [];
    W1_lambda_list = [];
    W1_sigma_list = [];
    W1_phase_list = [];
    W1_x0_list = [];
    W1_y0_list = [];
    W1_sse_list = [];
    W2_theta_list = [];
    W2_lambda_list = [];
    W2_sigma_list = [];
    W2_phase_list = [];
    W2_x0_list = [];
    W2_y0_list = [];
    W2_sse_list = [];

    W1t = W1';
    W2t = W2';

    for i = 1:160
        results = autoGaborSurf(xi,yi,reshape(W1t(:,i), [16 16]));
        W1_theta_list = [W1_theta_list results.theta];
        W1_lambda_list = [W1_lambda_list results.lambda];
        W1_sigma_list = [W1_sigma_list results.sigma];
        W1_phase_list = [W1_phase_list results.phase];
        W1_x0_list = [W1_x0_list results.x0];
        W1_y0_list = [W1_y0_list results.y0];
        W1_sse_list = [W1_sse_list results.sse];

        results = autoGaborSurf(xi,yi,reshape(W2t(:,i), [16 16]));
        W2_theta_list = [W2_theta_list results.theta];
        W2_lambda_list = [W2_lambda_list results.lambda];
        W2_sigma_list = [W2_sigma_list results.sigma];
        W2_phase_list = [W2_phase_list results.phase];
        W2_x0_list = [W2_x0_list results.x0];
        W2_y0_list = [W2_y0_list results.y0];
        W2_sse_list = [W2_sse_list results.sse];
    end

    save ../results/xica.mat
    
case 4, 
    
    load ../results/xica.mat;

    idx1 = find(log10(1+abs(W1_lambda_list)) < 3);
    idx2 = find(log10(1+abs(W2_lambda_list)) < 3);
    figure; 
    h = polar(rem(abs(W1_theta_list(idx1)),pi),log10(1+abs(W1_lambda_list(idx1))),'ob');
    % set(h,'MarkerFaceColor','b');
    % hold on;
    % h = polar(rem(abs(W2_theta_list(idx2)),pi),log10(1+abs(W2_lambda_list(idx2))),'xr');
    % set(h,'MarkerFaceColor','r');
    hold off;
    ylim([0 3]);  
    th = findall(gcf,'Type','text');
    for i = 1:length(th),
    set(th(i),'FontSize',24)
    end

    figure;
    h = scatter(W1_x0_list, W1_y0_list, 'ob');
    % set(h,'MarkerFaceColor','b');
    % hold on;
    % h = scatter(W2_x0_list, W2_y0_list, 'xr'); 
    % set(h,'MarkerFaceColor','r');
    hold off;
    set(gca,'FontSize',24);
    axis square; axis tight;
    box on;

    idx1 = find(abs(W1_sigma_list) < 5);
    idx2 = find(abs(W2_sigma_list) < 5);
    figure; 
    h = polar(rem(abs(W1_theta_list(idx1)),pi),abs(W1_sigma_list(idx1)),'ob');
    % set(h,'MarkerFaceColor','y');
    % hold on;
    % h = polar(rem(abs(W2_theta_list(idx2)),pi),abs(W2_sigma_list(idx2)),'xr');
    % set(h,'MarkerFaceColor','y');
    hold off;
    ylim([0 5]); 
    th = findall(gcf,'Type','text');
    for i = 1:length(th),
    set(th(i),'FontSize',24)
    end
end  
  

  
